package toolNet
{
	import engine.data.ModelLocator;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.display.Sprite;
	import flash.system.System;
	
	import newx.StageInfo;
	import newx.utils.debug.IO;

	public class ToolLogic
	{
		private var _model	:	Model;
		private var model:ModelLocator;
		public function ToolLogic()
		{
			_model	=	Model.getInstance();
			model				=	ModelLocator.getInstance();
		}
		
		public function logic(msgId : String, pList : Array):void
		{
			switch(msgId)
			{
				case CustomToToolFlag.START_TOOL:
				{//启动工具
					startTool();
					break;
				}
				case CustomToToolFlag.GAME_ROLE:
				{//获取游戏中玩家数据
					getRoleObject();
					break;
				}
				case CustomToToolFlag.GAmE_ROLE_GM:
				{//获取游戏中玩家数据
					getRoleGMObject();
					break;
				}
				case CustomToToolFlag.GAME_SCENE:
				{//获取游戏中场景数据
					getScenObject()
					break;
				}
				case CustomToToolFlag.GM_COMMAND:
				{//发送gm命令
					sendGmCommand(pList[0]);
					break;
				}
				case CustomToToolFlag.SERVER_MSG:
				{//发送给服务端的自定义消息
					sendServerMsg(pList);
				}
				case CustomToToolFlag.CLIENT_MSG:
				{//模拟服务端给客户端发消息
					sendClientMsg(pList);
				}
				case CustomToToolFlag.AIDE_INFO:
				{//辅助信息
					getAideInfo();
					break;
				}
			}
		}
		
		//启动工具
		private function startTool():void
		{
			_model.startTool();
		}
		
		//获得玩家数据
		public function getRoleObject():void
		{
			var dataModel 	:	ModelLocator	=	ModelLocator.getInstance();
			var role		:	Object			=	dataModel.scene.Role;
			_model.service.roleData(role);
		}
		
		//获得玩家数据
		public function getRoleGMObject():void
		{
			var dataModel 	:	ModelLocator	=	ModelLocator.getInstance();
			var role		:	Object			=	dataModel.scene.Role;
			_model.service.roleGMData(role);
		}
		
		public function getScenObject():void
		{
			
			var dataModel 	:	ModelLocator	=	ModelLocator.getInstance();
			var scene		:	Object			=	dataModel.scene;
			_model.service.sceneData(scene);
		}
		
		//发送gm命令
		private function sendGmCommand(command : String):void
		{
			var commands : Array	=	command.split(",");
			for each(var gmCommand : String in commands)
			{
				SystemSender.getInstance().SendGMCommand(gmCommand);
			}
		}
		
		
		/**
		 *给服务端发自定义消息 
		 * @param commands
		 * 
		 */
		private function sendServerMsg(commands : Array):void
		{
			IO.traceError(commands[0], commands[1]);
			
			if(commands[0]	==	CustomToServerFlag.CLIENT_CUSTOMMSG_GM)
			{
				SystemSender.getInstance().SendGMCommand( commands[1].toString());
				return;
			}
			SystemSender.getInstance().CustomMsgEx(commands[0], commands[1]);
		}
		
		
		/**
		 *模拟服务端给客户端发消息 
		 * @param commands
		 * 
		 */
		private function sendClientMsg(commands : Array):void
		{
			IO.traceError(commands[0], commands[1]);
			model.dispatchEvent(new CustomMsgEvent(String(commands[0]),commands[1]));
		}
		
		
		private function getAideInfo():void
		{
			var aideInfo	:	Object	=	{};
//			aideInfo.mc					=	countSceneMc();
			aideInfo.memory				=	countMemory();
			aideInfo.version			=	countVersion();
			
			_model.service.aideInfo(aideInfo);
		}
		
//		//统计场景mc(数量，名字)
//		private function countSceneMc():int
//		{
//			//统计mainUI上的mc数量
//			return countMc(MainUI.getInstance() + NormalSceneView.getInstance());
//		}
		
		private function countMc(parentMc : Sprite):int
		{
			var num 	:	int	=	0;
			
			var child	:	Sprite; 
			while(parentMc.numChildren > 0)
			{
				child	=	parentMc.getChildAt(0) as Sprite;
				if(child.numChildren > 0)
				{
					num +=	countMc(child);
				}else
				{
					num++;
				}
			}
			
			return num;
		}
		
		//统计cpu
		private function countCpu():void
		{
			
		}
		
		//统计内存
		private function countMemory():int
		{
			return System.totalMemory;
		}
		
		//统计版本
		private function countVersion():String
		{
			return StageInfo.version;
		}
	}
}